title 'JOP2.sas, Table 4 for Hensel-Goertz-Diehl JOP paper Table 4';
options linesize=80 pagesize=60;

/*Note that users will need to update this file to list the correct location
of the SAS data sets to be used; this file currently uses the directory on Paul Hensel's
computer, and thus will not function on other computers unless this is corrected*/

/*Read in files to be used*/
data ersumgd;
  infile 'Hensel:Data:ersumgd.csv' delimiter=',';
  input gdnumber dyad begriv endriv evrivgd gdrivdur gdrivmid gdrivwar
    chgdix chgdixa demchg demchga demdix2b demdix2e;
proc sort;
  by evrivgd gdnumber;
*proc means;
run;

data erdemmid;
  infile 'Hensel:Data:erdemmid.csv' delimiter=',';
  input gdnumber dyad gdchron dispute begdate year enddate evrivgd
    alhost blhost lhosta lhostm chgdix chgdixa demchg demchga demdix2
    ademoc aautoc bdemoc bautoc;
if ademoc=-9 then ademoc=.;
if aautoc=-9 then aautoc=.;
if bdemoc=-9 then bdemoc=.;
if bautoc=-9 then bautoc=.;
proc sort;
  by evrivgd gdnumber;
*proc means;
run;

data erdemyr;
  infile 'Hensel:Data:erdemyr.csv' delimiter=',';
  input gdnumber dyad year evrivgd demdix2 chgdix chgdixa demchg demchga
    midyr afteryr rivbegyr rivendyr rivdecad aftdecad;
if afteryr>-1;
proc sort;
  by evrivgd gdnumber;
*proc means;
run;


/*Data manipulation and analyses for Table 4*/
data ersumgd1;
  set ersumgd;
keep gdnumber demdix2b;
proc sort;
  by gdnumber;
run;

data erbeg;
  set ersumgd;
keep gdnumber demdix2b begriv;
proc sort;
  by gdnumber;
run;

data erdemmid;
  set erdemmid;
mid=1;
keep gdnumber year evrivgd mid;
proc sort;
  by gdnumber;
run;

data erdemyr;
  set erdemyr;
keep gdnumber dyad year chgdix evrivgd demdix2 demchg afteryr;
proc sort;
 by gdnumber year;
run;
data erdemyr;
  merge erbeg erdemyr;
  by gdnumber;
begrivyr=int(begriv/10000);
first20=.;
  if begrivyr le year le (begrivyr+19) then first20=1;
  if year>(begrivyr+19) then first20=0;
drop begrivyr;
run;


data erdemmid;
  merge erdemmid ersumgd1;
  by gdnumber;
proc sort;
  by gdnumber year;
proc summary;
  id demdix2b;
  class gdnumber year;
  var mid;
  output out=sumout sum(mid)=mids;

data sumout;
  set sumout;
drop _type_ _freq_;
if _type_ lt 3 then delete;

data tempyr;
  merge erdemyr sumout;
  by gdnumber year;
if (evrivgd=2 or evrivgd=3) and demchg=1;

if mids=. then mids=0;
proc sort;
  by gdnumber;

/* code transition year */
data tempyr;
  set tempyr;
  by gdnumber;
/* new rivalry reinitialize retain variable */
if first.gdnumber=1 then rdemdix2=demdix2;
retain rdemdix2;

tr2dem=0; tr2auth=0;
/* dem-dem to mixed or zero */
if demdix2=2 and rdemdix2=1 then tr2dem=1;
if demdix2=2 and rdemdix2=0 then tr2dem=1;
/* mixed  or zero to dem-dem */
if demdix2=1 and rdemdix2=2 then tr2auth=1;
if demdix2=0 and rdemdix2=2 then tr2auth=1;

/* reset retain variable for next year */
rdemdix2=demdix2;
drop rdemdix2;

/* code transition period to democ*/
data tempyr;
  set tempyr;
  by gdnumber;
aftdmtr=.; tr2dem2=0;
/* new rivalry reinitialize retain variable */
if first.gdnumber=1 then do;
  transyr=.;
  end;
if tr2dem=1 then do;
  transyr=year; tr2dem2=1;
  end;
retain transyr;

if transyr ne . then aftdmtr=year-transyr;

/* check if next is not transition to democracy */
if aftdmtr=1 and tr2auth ne 1 then tr2dem2=1;
drop transyr aftdmtr;

/* code transition period to non-democ*/
data tempyr;
  set tempyr;
  by gdnumber;
aftdmtr=.; tr2auth2=0;
/* new rivalry reinitialize retain variable */
if first.gdnumber=1 then do;
  transyr=.;
  end;
if tr2auth=1 then do;
  transyr=year; tr2auth2=1;
  end;
retain transyr;

if transyr ne . then aftdmtr=year-transyr;

/* check if next is not transition to democracy */
if aftdmtr=1 and tr2dem ne 1 then tr2auth2=1;
drop transyr aftdmtr;


/* code each year as dem, mixed, transition to dem, or transition from dem;
Note that in this command file I am working with one transition year,
reflecting the first year that a dyad qualifies as joint democratic as
the year of transition.  I thus use "regyear1" here, although the rest of
this command file could be modified to use "regyear2" to use two
transition years (for the first year of joint democracy and the next year)*/

data tempyr;
  set tempyr;
regyear1=.;
  if demdix2=0 then regyear1=0;
  if demdix2=1 then regyear1=1;
  if demdix2=2 then regyear1=2;
  if tr2dem=1 then regyear1=3;
  if tr2auth=1 then regyear1=4;
regyear2=.;
  if demdix2=0 then regyear2=0;
  if demdix2=1 then regyear2=1;
  if demdix2=2 then regyear2=2;
  if tr2dem2=1 then regyear2=3;
  if tr2auth2=1 then regyear2=4;

/* code years after transition to joint democracy */
data tempyr;
  set tempyr;
  by gdnumber;
aftdmtr=.;
/* new rivalry reinitialize retain variable */
if first.gdnumber=1 then transyr=.;
if tr2dem=1 then transyr=year;
retain transyr;
/* stop counting if transition away from joint democracy */
if tr2auth=1 then transyr=.;

if transyr ne . then aftdmtr=year-transyr;

if 0 < aftdmtr < 6 then aftdm=1;
if 5 < aftdmtr < 11 then aftdm=2;
if 10 < aftdmtr < 16 then aftdm=3;
if aftdmtr > 15 then aftdm=4;

drop transyr;

/* code years after transition to non-joint democracy */
data tempyr;
  set tempyr;
  by gdnumber;
aftauttr=.;
/* new rivalry reinitialize retain variable */
if first.gdnumber=1 then transyr=.;
if tr2auth=1 then transyr=year;
retain transyr;
/* stop counting if transition away from non-joint democracy */
if tr2dem=1 then transyr=.;

if transyr ne . then aftauttr=year-transyr;

if 0 < aftauttr < 6 then aftaut=1;
if 5 < aftauttr < 11 then aftaut=2;
if 10 < aftauttr < 16 then aftaut=3;
if aftauttr > 15 then aftaut=4;


table4=.;
  if demdix2<2 then table4=0;
  if demdix2=2 then table4=1;
  if regyear1=3 then table4=2;
  if aftdm=1 then table4=3;
  if aftdm=2 then table4=4;
  if aftdm=3 then table4=5;
  if aftdm=4 then table4=6;
if table4=. then table4=9;
run;




data tempyr1;
  set tempyr;
proc sort;
  by evrivgd dyad year;
title2 'List of all demchange proto-rivalries';
proc print;
  var dyad year first20 mids table4 demdix2;
  where afteryr>-1 and demchg=1 and evrivgd=2;
run;
title2 'List of all demchange enduring rivalries';
proc print;
  var dyad year first20 mids table4 demdix2;
  where afteryr>-1 and demchg=1 and evrivgd=3;
run;




data tempyr1;
  set tempyr;
proc sort;
  by evrivgd table4;
proc summary;
  class evrivgd table4;
  output out=sumdat n(chgdix)=totyr sum(mids)=mids;
data sumdat;
  set sumdat;
if _type_ lt 2 then delete;
drop _type_ _freq_;
pcmid=mids/totyr;
proc sort;
  by evrivgd table4;
title2 'Table 4 (PR): not dem/dem since start/transition yr/1-5/6-10/11-15/16+ since chg';
proc print noobs;
  var evrivgd table4 mids totyr pcmid;
  where evrivgd=2;
run;
title2 'Table 4 (ER): not dem/dem since start/transition yr/1-5/6-10/11-15/16+ since chg';
proc print noobs;
  var evrivgd table4 mids totyr pcmid;
  where evrivgd=3;
run;



data tempyr1;
  set tempyr;
proc sort;
  by table4;
proc summary;
  class table4;
  output out=sumdat n(chgdix)=totyr sum(mids)=mids;
data sumdat;
  set sumdat;
drop _type_ _freq_;
pcmid=mids/totyr;
title2 'Table 4 (all): not dem/dem since start/transition yr/1-5/6-10/11-15/16+ since chg';
proc sort;
  by table4;
proc print noobs;
  var table4 mids totyr pcmid;
run;



data tempyr1;
  set tempyr;
proc sort;
  by evrivgd table4;
proc summary;
  class first20 evrivgd table4;
  output out=sumdat n(chgdix)=totyr sum(mids)=mids;
data sumdat;
  set sumdat;
if _type_ lt 2 then delete;
drop _type_ _freq_;
pcmid=mids/totyr;
title2 'Table 4 supplement (split by 1st 20 years of rivalry, ERs only)';
proc sort;
  by first20 evrivgd table4;
proc print noobs;
  var evrivgd table4 mids totyr pcmid;
  where evrivgd=3 and first20 ne .;
  by first20;
run;
